package org.core.tliaswebmanagement.controller;

import lombok.extern.slf4j.Slf4j;
import org.core.tliaswebmanagement.pojo.PageResult;
import org.core.tliaswebmanagement.pojo.Result;
import org.core.tliaswebmanagement.pojo.Student;
import org.core.tliaswebmanagement.pojo.StudentQueryParam;
import org.core.tliaswebmanagement.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam param) {
        log.info("分页查询，参数：{}", param);
        PageResult<Student> pageResult = studentService.page(param);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@RequestBody Student student) {
        log.info("新增学生，数据：{}", student);
        studentService.add(student);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除学生，学生id：{}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询学生，学生id：{}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody Student student) {
        student.setUpdateTime(LocalDateTime.now());
        log.info("更新学生信息：{}", student);
        studentService.update(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id,@PathVariable Integer score){
        if(score<=0){return Result.error("违规分值必须大于0");}
        log.info("更新学生违规信息：{}", id);
        studentService.updateViolation(id,score);
        return Result.success();
    }
}
